library(gdata)
print(5)
setwd("/users/brucedesmarais/Dropbox/SenPE/Data/")
library(foreign)
sendat <- read.dta("senator_data.dta")

dat <- list()
 files <- c("form0304fnl.xls","form0506fnl.xls","form0708fnl.xls","form7980fnl.xls", "form8182fnl.xls","form8384fnl.xls", "form8586fnl.xls", "form8788fnl.xls", "form8990fnl.xls", "form9192fnl.xls", "form9394fnl.xls", "form9596fnl.xls", "form9798fnl.xls")


for(i in 1:length(files)){
	dat[[i]] <- read.xls(paste("./PEDat/",files[i],sep=""),stringsAsFactors=F, na.strings=c("NA",""))
}

# Names of senator columns in PE Data
scols <- paste("s",1:15,sep="")
# Congresses of dat
congs <- c(108,109,110,96+0:9)

# IDs for matching
id_master <- read.csv("id_master.csv",stringsAsFactors=F)


# Create the event Matrices

procName <- function(x){
	xvec <- character(length(x))
	for(i in 1:length(x)){
		chari <- x[i]
		comind <- which(strsplit(chari,split="")[[1]]==",")
		if(length(comind)==1){
		if(comind +1 < nchar(chari)){
			chari <- substr(chari,1,comind+2)
			}
			}
		xvec[i] <- chari
		}
		xvec
	}

mats <- list()

nattend <- NULL
ind <- 1
ifow <- c(1,4:13)
for(i in 1:length(dat)){
	dati <- dat[[i]]
	congi <- congs[i]
	mati <- NULL
	if(is.element(i,ifow)){
	idsi <- id_master[which(id_master[,1]==congi),5]
	idsi <- idsi[which(!is.na(idsi))]
	idsi <- procName(idsi)
	}
	if(!is.element(i,ifow)){
		idsi <- as.character(unique(c(as.matrix(dati[,scols]))))
		idsi <- idsi[which(!is.na(idsi))]
		}
	veci <- numeric(length(idsi))
	issue <- NULL
	for(j in 1:nrow(dati)){
		if(!is.na(dati$s1[j])){
			attend <- dati[j,scols]
			attend <- as.character(attend[which(!is.na(attend))])
			nattend <- c(nattend,length(attend))
			print(attend)
			attend <- procName(attend)
			vecij <- veci
			vecij[match(attend,idsi)] <- 1
			if(mean(vecij)!=0){
			mati <- rbind(mati,vecij)
			issue <- c(issue,dati$issue[j])
			}
			}
		}
		colnames(mati) <- idsi
		rownames(mati) <- issue
		mats[[ind]] <- mati
		ind <- ind+1
	}

wnets <- list()
topicNets <- list()
# Create Weighted Networks
for(i in 4:13){
	mati <- mats[[i]]
	neti <- matrix(0,ncol(mati),ncol(mati))
	tneti <- list()
	for(j in 1:nrow(neti)){
		tnetj <- list()
		for(k in (1:nrow(neti))[-j]){
			neti[j,k] <- sum(mati[,j]*mati[,k])
			ijrow <- which(mati[,j]*mati[,k]==1)
			tnetj[[k]] <- rownames(mati)[ijrow]
			}
			tneti[[j]] <- tnetj
		}
		colnames(neti) <- colnames(mati)
		rownames(neti) <- colnames(mati)
		wnets[[i-3]] <- neti
		topicNets[[i-3]] <- tneti
	}
	names(wnets) <- 96:105
	names(topicNets) <- 96:105


